//
//  WelcomeViewController.m
//  Hui
//
//  Created by mac on 17/2/17.
//  Copyright © 2017年 Hui. All rights reserved.
//

#import "WelcomeViewController.h"
#import "MainTabbarController.h"
#import "LoginViewController.h"
#import "JSPageControl.h"

#define IMAGECOUNT 3

@interface WelcomeViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) JSPageControl *pageControl;
@property (nonatomic, strong) UserDefaultsManager *userManager;
@property (nonatomic, assign) BOOL isScrollOut;

@end

@implementation WelcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.userManager = [UserDefaultsManager sharedInstance];
    //创建ScrollView
    UIScrollView *sv = [[UIScrollView alloc] init];
    sv.frame = self.view.bounds;
    //设置边缘不弹跳
    sv.bounces = NO;
    //整页滚动
    sv.pagingEnabled = YES;
    sv.showsHorizontalScrollIndicator = NO;
    _isScrollOut = YES;
    //加入多个子视图(ImageView)
    for(NSInteger i = 0; i < IMAGECOUNT; i++){
        NSString *imageName = [NSString stringWithFormat:@"launch%ld", i + 1];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = image;
        [sv addSubview:imageView];
        
        if(i == IMAGECOUNT-1) {
            //开启图片的用户点击功能
            imageView.userInteractionEnabled = YES;
            UIButton *enterButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 2 + (SCREEN_WIDTH - 200)/2 ,SCREEN_HEIGHT / 3 * 2, 200, SCREEN_HEIGHT / 3)];
            [enterButton addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
            [sv addSubview:enterButton];
        }
    }
    sv.contentSize = CGSizeMake(IMAGECOUNT * sv.frame.size.width, sv.frame.size.height);
    [self.view addSubview:sv];
    [self.view addSubview:self.pageControl];
    sv.delegate = self;
}
#pragma mark - get
- (JSPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[JSPageControl alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 85 - 15, SCREEN_WIDTH, 15)];
        _pageControl.thumWidth = 15;
        _pageControl.thumHeight = 15;
        _pageControl.thumsDistance = 10;
        _pageControl.currentPageTintColor = [UIColor colorWithHexString:@"0CA6F0"];
        _pageControl.thumPageTintColor = [UIColor colorWithHexString:@"CCCCCC"];
        _pageControl.cornerValue = 7.5;
        _pageControl.thumborderWidth = 1;
        _pageControl.thumBorderColor = [UIColor colorWithHexString:@"CCCCCC"];
        _pageControl.numberOfPages = IMAGECOUNT;
    }

    return _pageControl;
    
}


- (void)enter
{
    UIWindow *window = [AppDelegate sharedAppDelegate].window;
    if (!self.userManager.user_id) {
        LoginViewController *LoVC = [[LoginViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:LoVC];
        window.rootViewController = nav;
    }else{
        MainTabbarController *tabBar = [[MainTabbarController alloc] init];
        [self presentViewController:tabBar animated:YES completion:nil];
        window.rootViewController = tabBar;
    }
}

//UIScrollViewDelegate方法
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    int cuttentIndex = (int)(scrollView.contentOffset.x + SCREEN_WIDTH/2)/SCREEN_WIDTH;
    if (cuttentIndex == 2) {
        if ([self isScrolltoLeft:scrollView]) {
            if (!_isScrollOut) {
                return ;
            }
            [self enter];
        }
        self.pageControl.hidden = YES;
    }else{
        self.pageControl.hidden = NO;
    }
}
-(BOOL )isScrolltoLeft:(UIScrollView *) scrollView{
    //返回YES为向左反动，NO为右滚动
    if ([scrollView.panGestureRecognizer translationInView:scrollView.superview].x < 0) {
        return YES;
    }else{
        return NO;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    if(offset.x<=0){
        offset.x = 0;
        scrollView.contentOffset = offset;
    }
    NSUInteger index = round(offset.x / scrollView.frame.size.width);
    self.pageControl.currentPage = index;
    if (index == IMAGECOUNT - 1) {
        self.pageControl.hidden = YES;
    } else {
        self.pageControl.hidden = NO;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
